/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.samples;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.persistence.*;
import org.balisunrise.annotation.*;
import org.hibernate.annotations.Type;

/**
 *
 * @author Glauco
 */
@Entity
// <editor-fold defaultstate="collapsed" desc="VisualEntity">
@VisualEntity(transactions = {
    @Transaction(editor = "", search = "", title = "Cadastro de Paises")},
editors = {
    @Editor(panel = "", title = "País")},
searchs = {
    @Search(filter = "", grid = "", title = "Pesquisa de Paises"),
    @Search(alias = "selection", filter = "", grid = "embedded", title = "Pesquisa de Paises")},
panels = {
    @Panel(title = "País"),
    @Panel(alias = "selection", title = "País"),
    @Panel(alias = "emb", title = "País")},
grids = {
    @Grid(order = "byNome", height = 240, title = "Paises"),
    @Grid(alias = "embedded", title = "Paises")},
filters = {
    @Filter(title = "Filtro de Paises")})
// </editor-fold>
public class Pais implements Serializable{

    // <editor-fold defaultstate="collapsed" desc="Property Change Support">
    @Transient
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
    public static final String PROP_BACEN = "bacen";
    public static final String PROP_NOME = "nome";
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="annotations (ID)">
    @Id
    @Basic
    @PanelField(definitions =
    @FieldDefinition(label = "Código BACEN"))
    @GridColumn(definitions =
    @ColumnDefinition(name = "Bacen", index = 2, width = 32))
    // </editor-fold>
    private Short bacen;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length = 60, columnDefinition = "char(60)")
    @Type(type = "org.balisunrise.dao.hibernate.TrimmedString")
    @Selection(aliasForFields = {"selection"}, filterType = FilterType.ILIKE)
    @PanelField(definitions = {
        @FieldDefinition(label = "Nome do País"),
        @FieldDefinition(alias = {"selection", "emb"}, label = "País")})
    @FilterCriteria(definitions =
    @CriteriaDefinition(label = "Nome do País", filterType = FilterType.ILIKE))
    @GridColumn(definitions = {
        @ColumnDefinition(name = "Nome do País", index = 1, width = 320),
        @ColumnDefinition(alias = "embedded", name = "País")})
    // </editor-fold>
    private String nome;

    public Pais() { }

    // <editor-fold defaultstate="collapsed" desc="Get's & Set's">
    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {

        if (nome == null || nome.length() == 0) {
            throw new RuntimeException("Nome do País deve ser informado!");


        }
        String oldNome = this.nome;
        this.nome = nome;
        propertyChangeSupport.firePropertyChange(PROP_NOME, oldNome, nome);
    }

    public Short getBacen() {
        return bacen;
    }

    public void setBacen(Short bacen) {

        if (bacen == null || bacen == 0) {
            throw new RuntimeException("Código BACEN deve ser maior que zero!");


        }
        Short oldBacen = this.bacen;
        this.bacen = bacen;
        propertyChangeSupport.firePropertyChange(PROP_BACEN, oldBacen, bacen);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Pais other = (Pais) obj;
        if (this.bacen != other.bacen && (this.bacen == null || !this.bacen.equals(other.bacen))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 19 * hash + (this.bacen != null ? this.bacen.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "Pais[" + "bacen = \"" + bacen + "\" nome = \"" + nome + "\"]";
    }
    // </editor-fold>

}
